<template>
	<div class="system-role-container layout-padding">
		<div class="system-role-padding layout-padding-auto layout-padding-view">
			<div class="system-user-search mb15">
				<el-input v-model="state.tableData.param.search" size="default" placeholder="请输入用户名称" style="max-width: 180px"> </el-input>
				<el-button size="default" type="primary" class="ml10" @click="query()">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<!-- <el-button size="default" type="success" class="ml10" @click="onOpenAddRole('add')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增用户
				</el-button> -->
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column type="index" label="序号" width="60" />
				<!-- <el-table-column prop="id" label="授权编号" show-overflow-tooltip></el-table-column> -->
				<!-- <el-table-column prop="userId" label="用户编号" show-overflow-tooltip></el-table-column> -->
				<el-table-column prop="account" label="用户账号" show-overflow-tooltip></el-table-column>
				<el-table-column prop="userName" label="用户名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="del" label="用户状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.del">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<!-- <el-table-column prop="roleId" label="角色编号" show-overflow-tooltip></el-table-column> -->
				<el-table-column prop="role" label="角色标识" show-overflow-tooltip></el-table-column>
				<el-table-column prop="roleName" label="角色名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="roleIntro" label="角色描述" show-overflow-tooltip></el-table-column>
				<el-table-column prop="roleStatus" label="角色状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.roleStatus">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="authorizeTime" label="授权时间" show-overflow-tooltip></el-table-column>
				<el-table-column prop="authorizeStatus" label="授权状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.authorizeStatus">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="100">
					<template #default="scope">
						<el-button :disabled="scope.row.roleName === ''" size="small" text type="primary" @click="onOpenEditRole('edit', scope.row)"
							>修改</el-button
						>
						<el-button size="small" text type="primary" @click="onRowDel(scope.row)" v-if="scope.row.authorizeStatus">删除</el-button>
						<el-button size="small" text type="primary" @click="onEnable(scope.row)" v-else>重启</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="state.tableData.param.pageNum"
				background
				v-model:page-size="state.tableData.param.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total"
			>
			</el-pagination>
		</div>
		<RoleDialog ref="roleDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup name="systemRole">
import { ElMessageBox, ElMessage } from 'element-plus';
import axios from 'axios';
import api from "/@/api.js";
// 引入组件
const RoleDialog = defineAsyncComponent(() => import('/@/views/system/userRole/dialog.vue'));

// 定义变量内容
const roleDialogRef = ref();
const state = reactive({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			search: '',
			pageNum: 1,
			pageSize: 10,
		},
	},
});

// 根据用户名查询用户
const query = () => {
	if(state.tableData.param.search === ""){
		ElMessage.error("请输入用户名");
		return;
	}

	axios({
		method: 'get',
		url: api.commonUrl + '/userRole/getbyusername?userName=' + state.tableData.param.search,
	}).then(function (response) {
		if(response.data.code === 200){
			state.tableData.loading = true;
			const data = [];
			for (let i = 0; i < response.data.data.userRoleList.length; i++) {
				data.push({
					id: response.data.data.userRoleList[i].id,
					userId:response.data.data.userRoleList[i].userId,
					account: response.data.data.userRoleList[i].account,
					userName: response.data.data.userRoleList[i].userName,
					del: response.data.data.userRoleList[i].del === '1' ? true : false,
					roleId: response.data.data.userRoleList[i].roleId,
					role: response.data.data.userRoleList[i].role,
					roleName: response.data.data.userRoleList[i].roleName,
					roleIntro: response.data.data.userRoleList[i].roleIntro,
					roleStatus: response.data.data.userRoleList[i].roleStatus === '1' ? true : false,
					authorizeTime: response.data.data.userRoleList[i].authorizeTime,
					authorizeStatus: response.data.data.userRoleList[i].authorizeStatus === '1'? true : false,
				});
			}
			state.tableData.data = data;
			//console.log(response.data.data.total);
			state.tableData.total = response.data.data.total;
			//state.tableData.param.search = '';
			setTimeout(() => {
				state.tableData.loading = false;
			}, 500);
		}else{
			ElMessage.error(response.data.message);
		}
		
	});
	un.value = '';
};

// 初始化表格数据
const getTableData = () => {

	axios({
		method: 'post',
		url: api.commonUrl + '/userRole/getalluserrolebypage',
		// data: {
		// 	page: 1,
		// 	pageSize: 10,
		// },
		data: `page=` + state.tableData.param.pageNum + `&pageSize=` + state.tableData.param.pageSize,
	}).then(function (response) {
			if(response.data.code === 200){
				state.tableData.loading = true;
				const data = [];
				for (let i = 0; i < response.data.data.records.length; i++) {
					//console.log(user);
					data.push({
						id: response.data.data.records[i].id,
						userId:response.data.data.records[i].userId,
						account: response.data.data.records[i].account,
						userName: response.data.data.records[i].userName,
						del: response.data.data.records[i].del === '1' ? true : false,
						roleId: response.data.data.records[i].roleId,
						role: response.data.data.records[i].role,
						roleName: response.data.data.records[i].roleName,
						roleIntro: response.data.data.records[i].roleIntro,
						roleStatus: response.data.data.records[i].roleStatus === '1' ? true : false,
						authorizeTime: response.data.data.records[i].authorizeTime,
						authorizeStatus: response.data.data.records[i].authorizeStatus === '1'? true : false,
					});
				}
			state.tableData.data = data;
			state.tableData.total = response.data.data.total;
			setTimeout(() => {
				state.tableData.loading = false;
			}, 500);
		}else{
			ElMessage.error(response.data.message);
		}
	});
};

// 打开新增角色弹窗
const onOpenAddRole = (type) => {
	roleDialogRef.value.openDialog(type);
};
// 打开修改角色弹窗
const onOpenEditRole = (type, row) => {
	roleDialogRef.value.openDialog(type, row);
};
// 删除角色
const onRowDel = (row) => {
	ElMessageBox.confirm(`此操作将删除该记录，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			axios(
			{
				method: 'post',
				url: api.commonUrl + '/userRole/deleteuserrolebyid',
				data: {
					id: row.id,
				},
				headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
			}
		)
		.then(function (response) {
			ElMessage.success('删除成功');
			getTableData();
		});
		})
		.catch(() => {});
};

// 重启用户
const onEnable = (row) => {
	//alert(row);
	ElMessageBox.confirm(`此操作将重启该记录，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			axios({
				method: 'post',
				url: api.commonUrl + '/userRole/restartuserrolebyid',
				data: `id=` + row.id,
			}).then(function (response) {
				if (response.data.code == 200) {
					getTableData();
					ElMessage.success(response.data.message);
				} else {
					ElMessage.error(response.data.message);
				}
			});
		})
		.catch(() => {});
};

// 分页改变
const onHandleSizeChange = (val) => {
	state.tableData.param.pageSize = val;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (val) => {
	state.tableData.param.pageNum = val;
	getTableData();
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
.system-role-container {
	.system-role-padding {
		padding: 15px;
		.el-table {
			flex: 1;
		}
	}
}
</style>
